package main

import (
	"context"
	"github.com/wechatpay-apiv3/wechatpay-go/core"
	"github.com/wechatpay-apiv3/wechatpay-go/core/option"
	"github.com/wechatpay-apiv3/wechatpay-go/services/payments/native"
	"github.com/wechatpay-apiv3/wechatpay-go/utils"
	"log"
	"time"
)

func main() {
	var (
		mchID                      = "1503542261"                               // 商户号
		mchCertificateSerialNumber = "279A988FDDC9C16DF0B1738C0BAF415D2DD9305B" // 商户证书序列号
		mchAPIv3Key                = "QuwXu5aslCMERJhNzc89KMZCLGxSjRYj"         // 商户APIv3密钥
		appId                      = "wxdb15fcb86d858052"                       // appid
	)
	// 使用 utils 提供的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
	privateKey := "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDdNvuSOsRCMg/d\n83tbKhIBAUcKLMN8ntqXME7yQdGsuEOnC8Ie8h/9mG+BTFVF86qV0IJ5FoxgUSwd\nHhzmdkOycivSpqUD8nkAHoAcKHUaYko62yACto5c0ISVAgDD4u+z0yK1TqGfUYZ3\naLBPAi6wwCwu7Z02rngpEgApi4tzXc7t1vcOaSFRinHjc/LokNIlH1aDlLEfczaO\nKQMmuPUR0Xseqjeq+alLPOzkCWv4diEnSbJPPenw0CIIpSVxvGlto4UW94Aqgcab\naNCjYS3Ym9eJcjr1va3UCuZKlZlW7xFzeS0ycDV1T5GoE+Ohl9WT6Md86KJuDFZB\no/3bS2u/AgMBAAECggEAS+xcCYMr6sA92+J+OvrEdR3YIPDeIz5uLafB4LmgWhRf\n+8w4UuCtBB+2XX0yYzDPIl4O+bo4cbJKXrAAIUUD1RIEYmcZibJFvzm4pHFqR6Tm\nby7X3LaFQ+u5JV8rng+cT26uoeWb6Q9VBFSk9mj8V2ONhom9BJB64zWvYpLnSK+N\nQc5iGZbFHdepkzG3I0v6c6PwKg3ZhGdMzG3V0JYf7p7YBRH7wZQki32MLyzKyLaD\nsR0iP+6OW9aeMd+FA2Ywe7tTkUPfS0XAegFb1C+YwKI4GTldzdX5hjKiQLB+J+qR\nGU8MQtHubZXVZTKcD30EmSEMLnfDK7YPE65DDJAM8QKBgQDw1iwBiYGiK0CxfMBk\n1VWz/bc5MIJg917Ou0+c8t2PzWeqMOdSCE8aSOEIhfIG4ikZqlDVtP7qkUfylaYj\nzra5CamQ+msm1aso7ywQc1r3jVp+WcEQbp/GHyttNVdFujDNb504wxdqXs6d5O9u\nQMQT0asof7SBcXuPOQU41kO2xwKBgQDrJItPBayLd/OaA3y0Ym6k1Vq1pvOiSuuF\nhURV18KUEfN9GE39diTH/P9PCVLXB/fqRsyM1IJfJHl9303SxPtVV0P/PFe3InPl\nr2c3YMEVKQsYwGlfWKBsBhaWa5uZKKsWhn4HmkN8EqpM/dAkjAee3Kko20PifIL/\nQqZRiRBLSQKBgA8ZjobGJU+SoKrAJJvt2ELBvJQkW3hS6inSCqrPe17BxSchi89T\n4AnI2fsb0tHlqigGyqyrfXQdicJG6bTDCCfP2EbTvJYhvWqCnGOnp+NvhGX5oKY0\nGWZlm+Zr+r3Pb7HhtpzRdo1fxlqV+QETpHeNpMqUPzACenm/XHl0zagPAoGAMWyB\n938a0EnLIxEekg+qb+0EXvATf4Y90Ne9EEoEdPj888s7AU5m1r4/r44nIllja5Os\nBOHZ8UOKYcGg6u9KYJqyCSDW0wBj2eAvUr18eAUjdvVicI8BURNKhh1iHCQnKdnk\n/ZYYHykRMJOcv0HLjEGXMKY9s4o2Qo7wxGyJxzkCgYEAqIbbLZ2FQtK50GGWNJxs\nSsmTPH+N0Tu+Hrek6NOtX2ZTER9wbk87PVkNh0KA5li117dUD2p6/GA/wgHSK0RN\nIrgq70PBqOdBwyPOFYMtJ8+oq0y8AhzcXs9Est7nzV3RA9ybqu7Omo6iT6EV2lM8\nuALcsiODTjR/4tbOts+2/44=\n-----END PRIVATE KEY-----\n"
	mchPrivateKey, err := utils.LoadPrivateKey(privateKey)
	if err != nil {
		log.Fatal("load merchant private key error")
	}

	ctx := context.Background()
	// 使用商户私钥等初始化 client，并使它具有自动定时获取微信支付平台证书的能力
	opts := []core.ClientOption{
		option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
	}
	client, err := core.NewClient(ctx, opts...)
	if err != nil {
		log.Fatalf("new wechat pay client err:%s", err)
	}

	svc := native.NativeApiService{Client: client}
	expire := time.Now().Add(10 * time.Minute)
	resp, result, err := svc.Prepay(ctx,
		native.PrepayRequest{
			Appid:         core.String(appId),
			Mchid:         core.String(mchID),
			Description:   core.String("Image形象店-深圳腾大-QQ公仔"),
			OutTradeNo:    core.String("1217752501201407023333368018"),
			TimeExpire:    core.Time(expire),
			Attach:        core.String("自定义数据说明"),
			NotifyUrl:     core.String("https://www.weixin.qq.com/wxpay/pay.php"),
			SupportFapiao: core.Bool(false),
			Amount: &native.Amount{
				Currency: core.String("CNY"),
				Total:    core.Int64(5),
			},
		},
	)

	if err != nil {
		// 处理错误
		log.Printf("call Prepay err:%s", err)
	} else {
		// 处理返回结果
		log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
	}
}
